<?php
/**
 * 联系人
 * @return
 * @author yuncran@126.com
 */

namespace app\model;

use app\BaseModel;
use app\CacheKey;

class FriendModel extends BaseModel
{
    protected $name = 'friend';

    protected $pk = "friend_id";


    protected $append = ['friend_user'];

    public function getFriendUserAttr($value, $data)
    {
        $friend_user = self::$uid == $data['create_user'] ? $data['friend_user_id'] : $data['create_user'];
        return UserModel::where(['id' => $friend_user])->field('id,nickname,avatar,email,level,bio,verification')->append(['level_result'])
            ->withAttr('level_result', function ($value, $data) {
                return UserLevelModel::where(['id' => $data['level']])->cache(CacheKey::level_title)->find();
            })->find();
    }
}